import 'dart:convert';

import 'package:flutter/material.dart';
// import '../data/CustomData.dart';
import 'package:dio/dio.dart';
import 'package:flutter_app/detail/Detail.dart';

class MovieList extends StatefulWidget {
  @override
  _MovieListState createState() => _MovieListState();
}

class _MovieListState extends State<MovieList> {
  // final List infoList = new CustomData().infoList;
  List infoList = [];
  //页面还没加载的时候执行-系统行为
  @override
  void initState() {
    super.initState();
    getMovieList();
  }

  //请求数据
  getMovieList() async {
    try {
      Dio dio = Dio();
      Response response = await dio.get("https://getman.cn/mock/zq/test");
      setState(() {
        infoList = jsonDecode(response.toString());
      });
    } catch (e) {
      print(e);
    }
  }

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
      itemBuilder: (BuildContext context, int index) {
        Map _item = infoList[index];
        return GestureDetector(
          onTap: () {
            Navigator.of(context)
                .push(MaterialPageRoute(builder: (BuildContext context) {
              return Detail(nm: _item["nm"]);
            }));
          },
          child: Container(
            padding: EdgeInsets.all(10),
            child: Row(
              children: <Widget>[
                //左边图片
                Container(
                  child: Image(
                    image: NetworkImage('${_item["img"]}'),
                    height: 179,
                    width: 128,
                  ),
                ),
                //右边文字描述
                //Expanded 弹性盒子容器，可以占据多余空间
                Expanded(
                    child: Container(
                  padding: EdgeInsets.only(left: 10),
                  height: 160,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: <Widget>[
                      Text(
                        '${_item["nm"]}',
                        style: TextStyle(fontSize: 18),
                        overflow: TextOverflow.ellipsis,
                        maxLines: 1,
                      ),
                      Text('${_item["sc"]}'),
                      //？？非空判断，前者有值用前者，否则用后者
                      Text(
                        '${_item["star"] ?? "无"}',
                        overflow: TextOverflow.ellipsis,
                        maxLines: 1,
                      ),
                      Text('${_item["rt"]}'),
                      Text('${_item["showInfo"]}'),
                    ],
                  ),
                ))
              ],
            ),
          ),
        );
      },
      itemCount: infoList.length,
    );
  }
}
